package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PageTemplate;
import com.ruoyi.system.service.IPageTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.annotation.Anonymous;

/**
 * 页面模板Controller
 * 
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/system/pageTemplate")
public class PageTemplateController extends BaseController
{
    @Autowired
    private IPageTemplateService pageTemplateService;

    /**
     * 查询页面模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageTemplate pageTemplate)
    {
        startPage();
        List<PageTemplate> list = pageTemplateService.selectPageTemplateList(pageTemplate);
        return getDataTable(list);
    }

    /**
     * 导出页面模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:export')")
    @Log(title = "页面模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageTemplate pageTemplate)
    {
        List<PageTemplate> list = pageTemplateService.selectPageTemplateList(pageTemplate);
        ExcelUtil<PageTemplate> util = new ExcelUtil<PageTemplate>(PageTemplate.class);
        util.exportExcel(response, list, "页面模板数据");
    }

    /**
     * 获取页面模板详细信息
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('system:pageTemplate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pageTemplateService.selectPageTemplateById(id));
    }

    /**
     * 新增页面模板
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:add')")
    @Log(title = "页面模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageTemplate pageTemplate)
    {
        return toAjax(pageTemplateService.insertPageTemplate(pageTemplate));
    }

    /**
     * 修改页面模板
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:edit')")
    @Log(title = "页面模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageTemplate pageTemplate)
    {
        return toAjax(pageTemplateService.updatePageTemplate(pageTemplate));
    }

    /**
     * 删除页面模板
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:remove')")
    @Log(title = "页面模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pageTemplateService.deletePageTemplateByIds(ids));
    }

    /**
     * 复制页面模板
     */
    @PreAuthorize("@ss.hasPermi('system:pageTemplate:add')")
    @Log(title = "页面模板", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{id}")
    public AjaxResult copy(@PathVariable("id") Long id)
    {
        Long newId = pageTemplateService.copyPageTemplate(id);
        if (newId == null) {
            return error("复制模板失败，模板不存在");
        }
        return success(newId);
    }
}
